<template>
  <div class="page-list-live">
    <PageList>
      <template #header>人员基站管理</template>
      <template #filter>
        <ListFilter :form="topFilter" @search="handleSearch" @reset="handleReset"></ListFilter>
      </template>
      <template #tips>
        <el-button type="primary" icon="el-icon-document-add" @click="handleAdd">新建基站</el-button>
      </template>
      <template #table>
        <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol">
          <template slot="col1" slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              :loading="scope.row.loadingEdit"
              size="mini"
              style="margin-left: 0px; margin-right: 10px;"
              type="text"
              @click="handleRowEdit(scope.row)"
            >编辑</el-button>
            <el-button
              :loading="scope.row.loadingDel"
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color: red; margin-left: 0px; margin-right: 10px;"
              @click="handleRowDel(scope.row)"
            >删除</el-button>
          </template>
        </ListTable>
      </template>
    </PageList>
    <Device ref="RefAccountsEdit" @success="afterEditSuccess"></Device>
  </div>
</template>

<script>
  import listMixin from '@/mixins/list'
  import Device from './components/Device.vue'
  import ListFilter from '@/components/ListFilter'
  import { baseStation } from '@/api/api'
  export default {
    components: {
      Device,
      ListFilter
    },
    mixins: [listMixin],
    data() {
      return {
        topFilter: {
          deviceId: { label: '设备ID', placeholder: '请输入设备ID' },
          name: { label: '基站名称', placeholder: '请输入基站名称' }
        },
        tableData: [],
        tableCol: [
          { type: 'index', label: '序号', width: '60px' },
          { prop: 'deviceId', label: '设备ID', minWidth: 100 },
          { prop: 'name', label: '基站名称' },
          { prop: 'ip', label: 'IP', minWidth: 100 },
          {
            prop: 'siteType',
            label: '基站类型',
            formatter(row, col, cell) {
              if (row.siteType == 1) {
                return '进基站'
              } else if (row.siteType == 2) {
                return '出基站'
              } else {
                return '其它基站'
              }
            }
          },
          {
            prop: 'areaId',
            label: '区域',
            formatter(row, col, cell) {
              if (row.area) {
                return row.area.name
              } else {
                return ''
              }
            }
          },
          // {
          //   prop: 'isForWorkCheck',
          //   label: '是否是考勤机站',
          //   formatter(row, col, cell) {
          //     return row.isForWorkCheck ? '是' : '否'
          //   },
          //   minWidth: '124px'
          // },
          // { prop: 'frontDistance', label: '前定位点距离' },
          // { prop: 'backDistance', label: '后定位点距离' },
          // { prop: 'distanceFromEntrance', label: '离隧道口距离(米)' },
          { prop: 'status', label: '状态' },
          { label: '操作', custom: 'col1' }
        ]
      }
    },
    methods: {
      // 请求列表数据
      async getList(query) {
        this.tableLoading = true
        try {
          let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false }
          const {
            data: { items: resData, count, pageIndex }
          } = await baseStation('post', query, false)
          this.tableData = []
          this.tableData = resData.map(item => {
            item.loadingEdit = false
            item.loadingDel = false
            return item
          })
          this.$nextTick(() => {
            this.$refs['RefListTable'].update({ page: pageIndex, count })
            this.tableLoading = false
          })
        } catch (err) {
          this.tableLoading = false
          this.tableData = []
        }
      },
      async afterEditSuccess() {
        this.getList(this.serchObj)
      },
      handleAdd() {
        let objData = {
          // 进入哪一个程序
          status: 'add',
          apiKey: {
            add: '/BaseStation', // 添加
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
        this.$refs['RefAccountsEdit'].getList()
        this.$refs['RefAccountsEdit'].getTunnelData()
        this.$refs['RefAccountsEdit'].getIcon()
        this.$refs['RefAccountsEdit'].getEquipment()
      },
      handleRowEdit(row) {
        this.$refs['RefAccountsEdit'].getList()
        this.$refs['RefAccountsEdit'].getTunnelData()
        this.$refs['RefAccountsEdit'].getIcon()
        this.$refs['RefAccountsEdit'].getEquipment()
        let objData = {
          // 进入哪一个程序
          status: 'edit',
          // 源数据
          row: row,
          // 源id
          id: row.id,
          // 查询参数
          searchData: {
            url: '/BaseStation/' + row.id,
            method: 'GET',
            params: null
          }
        }
        this.$refs['RefAccountsEdit'].openOne(objData)
      },
      async delRowReq(row) {
        try {
          let obj = { id: row.id, value: null }
          await baseStation('delete', obj, true)
          row.loadingDel = false
          this.getList(this.serchObj)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (err) {
          row.loadingDel = false
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      },
      handleRowDel(row) {
        row.loadingDel = true
        this.$_delConfirm(
          '基站',
          () => {
            this.delRowReq(row)
          },
          () => {
            row.loadingDel = false
          }
        )
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
